/**
 *
 * User: rendong
 * Date: 12-9-3
 * Time: 下午10:29
 * $Id: GFunction.js 12 2012-09-10 16:47:05Z rendong237@gmail.com $
 */


Function.prototype.extend = function(parentObj){
    this.prototype = parentObj;
}

/**
 * 动画渲染函数
 * @type {requestAnimationFrame}
 */
window.requestAnimationFrame = function (){
    return window.requestAnimationFrame
        || window.mozRequestAnimationFrame
        || window.webkitRequestAnimationFrame
        || window.msRequestAnimationFrame
        || window.oRequestAnimationFrame
        || function(callback) {
        setTimeout(callback, 1000 / 60);
    }
}();


/**
 * 计算脏矩形
 */
function GCalcDirtyRect(dirtyRect, disp, topRect, stageWidth, stageHeight)
{
    var cur;
    var rect = new GRect(0,0,0,0);
    for(i=0,len=disp.numChild(); i < len; i++){
        cur = disp._childs[i];

        if (cur.isReDraw){
            rect.x = cur.boundRect.x + topRect.x;
            rect.y = cur.boundRect.y + topRect.y;
            rect.width = cur.boundRect.width + topRect.width;
            rect.height = cur.boundRect.height + topRect.height;

            if (rect.x< dirtyRect.x)
                dirtyRect.x = rect.x;

            if (rect.x +rect.width > dirtyRect.x +dirtyRect.width)
                dirtyRect.width = rect.x +rect.width - dirtyRect.x;

            if (rect.y < dirtyRect.y)
                dirtyRect.y = rect.y;

            if (rect.y + rect.height > dirtyRect.y + dirtyRect.height)
                dirtyRect.height = rect.y + rect.height - dirtyRect.y;

            if (dirtyRect.x < 0)
                dirtyRect.x = 0;

            if (dirtyRect.y < 0)
                dirtyRect.y = 0;

            if (dirtyRect.x + dirtyRect.width > stageWidth)
                dirtyRect.width = stageWidth - dirtyRect.x;

            if (dirtyRect.y + dirtyRect.height > stageHeight)
                dirtyRect.height = stageHeight - dirtyRect.y;

            // 如果脏区域 满屏则直接返回
            if(dirtyRect.x == 0 && dirtyRect.y == 0 && dirtyRect.width == stageWidth && dirtyRect.height == stageHeight)
                return;
        }

        // 计算子对象
        if (cur instanceof GDisplayObjectContainer){
            var newTopRect = new GRect(0,0,0,0);
            newTopRect.x = cur.x + topRect.x;
            newTopRect.y = cur.y + topRect.y;
            newTopRect.width = cur.width + topRect.width;
            newTopRect.height = cur.height + topRect.height;

            GCalcDirtyRect(dirtyRect, cur, newTopRect, stageWidth, stageHeight);
        }
    }


}